package com.kkd.kkdapp.activity;

import java.util.List;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.adapter.TwoFriendsAdapter;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.entity.TwoFriendsData;
import com.kkd.kkdapp.http.entity.TwoFriendsDataList;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.utils.PreferenceUtils;
import com.kkd.kkdapp.utils.SystemUtils;
import com.kkd.kkdapp.utils.Time_Date;
import com.kkd.kkdapp.view.KkdXlistView;
import com.kkd.kkdapp.view.KkdXlistView.KKDScrollListener;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 我的二度好友
 * 
 * @author mty
 * 
 */
public class TwoFriendsActivity extends BaseActivity implements
		OnClickListener, KKDScrollListener {

	private final int CODE_ZREO = 1;

	private RelativeLayout twofriends_img; // 返回键
	private KkdXlistView mXListView;
	private TwoFriendsAdapter mAdapter;
	private int pageNO = 1;
	private int pageSize = 10;
	// private String version;
	private String friendCustomerId;
	private List<TwoFriendsDataList> dataList;
	private boolean REFRESHFLAG = true;
	private ImageView img_null;
	private TextView tv_null;
	private ListView mListView;
	private TwoFriendsData data;

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {

			switch (msg.what) {
			case CODE_ZREO:
				int pageCount = data.getPageCount();
				int pageNo = data.getPageNO();
				if (pageNo > pageCount) {
					mXListView.stopLoadMore(-1);
					if (REFRESHFLAG) {
						mXListView.stopLoadMore(1);
					}
				} else {
					dataList = data.getDataList();
					mAdapter.update();
					mAdapter.appendData(dataList, pageNO == 1);
					mAdapter.notifyDataSetChanged();
					if (dataList.size() < pageSize) {
						mXListView.stopLoadMore(1);
					} else {
						mXListView.stopLoadMore(0);
					}
					mXListView.stopRefresh();
					Time_Date time_Date = new Time_Date();
					String time = time_Date.get_Year_Day();
					mXListView.setRefreshTime(time);
				}
				mXListView.stopRefresh();
				Time_Date time_Date = new Time_Date();
				String time = time_Date.get_Year_Day();
				mXListView.setRefreshTime(time);
				if (REFRESHFLAG) {
					if (dataList == null) {
						// mXListView.setPullLoadEnable(false);
						img_null.setVisibility(View.VISIBLE);
						tv_null.setVisibility(View.VISIBLE);
					} else {
						// mXListView.setPullLoadEnable(true);
						img_null.setVisibility(View.GONE);
						tv_null.setVisibility(View.GONE);
					}
				}
				break;

			}

		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_two_friends);
		initView();
		getIntentMessager();
		setListener();
	}

	/**
	 * 初始化组件
	 */
	public void initView() {
		twofriends_img = (RelativeLayout) this
				.findViewById(R.id.twofriends_img);
		mXListView = (KkdXlistView) this.findViewById(R.id.manageaward_lv);
		img_null = (ImageView) this.findViewById(R.id.img_null);
		tv_null = (TextView) this.findViewById(R.id.tv_null);

		mXListView.setKKDScrollListener(this);
		mListView = mXListView.mListView;
		mListView.setDividerHeight(0);
		mAdapter = new TwoFriendsAdapter(this);
		mListView.setAdapter(mAdapter);
		mXListView.startRefresh();
	}

	/**
	 * 注册监听
	 */
	public void setListener() {
		twofriends_img.setOnClickListener(this);
		// mXListView.setXListViewListener(this);
		// mXListView.setPullRefreshEnable(true);
		if (!AppUtil.isConnect(this)) {
			// mXListView.setPullLoadEnable(false);
			img_null.setVisibility(View.VISIBLE);
			tv_null.setVisibility(View.VISIBLE);
			img_null.setImageResource(R.drawable.img_null);
			tv_null.setText(R.string.network_anomalys);
		} else {
			// mXListView.setPullLoadEnable(false);
		}
	}

	public void getIntentMessager() {
		Intent intent = this.getIntent();
		friendCustomerId = intent.getStringExtra("customerId");
		quesetTwoFriends();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.twofriends_img:
			finish();
			break;
		}
	}

	/**
	 * 二度好友
	 */
	public void quesetTwoFriends() {

		KkdHttpMagager.getInstance().getTwoFriendsManager(pageNO, pageSize,  friendCustomerId,
				 TwoFriendsData.class, new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						data = (TwoFriendsData) responseBody;
						handler.sendEmptyMessage(CODE_ZREO);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						showToast(desc);
						handler.postDelayed(new Runnable() {

							@Override
							public void run() {
								showToast("加载失败");
								// mXListView.stopLoadMore(0);
								mXListView.stopLoadMore(1);
								mXListView.stopRefresh();
							}
						}, 0);
					}
				});
	}

	@Override
	public void onRefresh() {

		pageNO = 1;

		if (AppUtil.isConnect(TwoFriendsActivity.this)) {
			REFRESHFLAG = true;
			quesetTwoFriends();
		} else {
			mXListView.stopRefresh();
			showToast("网络连接失败！");
			if (dataList == null) {
				img_null.setVisibility(View.VISIBLE);
				tv_null.setVisibility(View.VISIBLE);
				tv_null.setVisibility(View.VISIBLE);
				img_null.setImageResource(R.drawable.img_null);
				tv_null.setText(R.string.network_anomalys);
			} else {
				img_null.setVisibility(View.GONE);
				tv_null.setVisibility(View.GONE);
			}
			handler.postDelayed(new Runnable() {

				@Override
				public void run() {
					showToast("加载失败");
					mXListView.stopLoadMore(0);
					mXListView.stopRefresh();
				}
			}, 1000);
		}
	}

	@Override
	public void onLoadMore() {

		if (AppUtil.isConnect(TwoFriendsActivity.this)) {
			pageNO++;
			quesetTwoFriends();
			REFRESHFLAG = false;
		} else {
			showToast("网络连接失败！");
			mXListView.stopLoadMore(0);
		}
	}

	// @Override
	// public void onRefresh() {
	// handler.postDelayed(new Runnable() {
	//
	// @Override
	// public void run() {
	// pageNO = 1;
	//
	// if (AppUtil.isConnect(TwoFriendsActivity.this)) {
	// REFRESHFLAG = true;
	// quesetTwoFriends();
	// } else {
	// mXListView.stopRefresh();
	// Toasttool.MyToast(TwoFriendsActivity.this, "网络连接失败！");
	// if (dataList == null) {
	// mXListView.setPullLoadEnable(false);
	// img_null.setVisibility(View.VISIBLE);
	// tv_null.setVisibility(View.VISIBLE);
	// tv_null.setVisibility(View.VISIBLE);
	// img_null.setImageResource(R.drawable.network_default);
	// tv_null.setText(R.string.network_anomalys);
	// } else {
	// mXListView.setPullLoadEnable(true);
	// img_null.setVisibility(View.GONE);
	// tv_null.setVisibility(View.GONE);
	// }
	// }
	// }
	// }, 1000);
	// }
	//
	// @Override
	// public void onLoadMore() {
	// handler.postDelayed(new Runnable() {
	//
	// @Override
	// public void run() {
	// if (AppUtil.isConnect(TwoFriendsActivity.this)) {
	// pageNO++;
	// quesetTwoFriends();
	// REFRESHFLAG = false;
	// } else {
	// Toasttool.MyToast(TwoFriendsActivity.this, "网络连接失败！");
	// mXListView.stopLoadMore(0);
	// }
	// }
	// }, 1000);
	//
	// }
}
